﻿/*********************************************************
** File Name:	DataProvider.cs
** Copyright (C) 2010 hiSoft ITDC. All right reserved.
** Creator:	Lionel Li
** Create date:	2010-05-26
** Modifier:	
** Modify date:	
** Description:	数据访问支持类
*********************************************************/
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;

namespace Common
{
    public class DataProvider
    {
        // singleton reference to the instantiated object
        private static Hashtable objProvider = new Hashtable();
        private string connectionString;
        private SqlTransaction transaction = null;
        private SqlConnection connection = null;

        // constructor
        static DataProvider()
        {
            //CreateProvider();
        }

        // dynamically create provider
        private void CreateProvider(string key)
        {
            //objProvider = (DataProvider)Reflection.CreateObject("data");
            connectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString;

            //string regexStr = "connection string=\"(?<key>.*?)\"";
            //Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
            //Match mc = r.Match(connectionString);
            //connectionString = mc.Groups["key"].Value;
        }

        // return the provider
        public static DataProvider Instance(string key)
        {
            if (objProvider[key] == null)
                objProvider[key] = new DataProvider();
            ((DataProvider)objProvider[key]).connectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString;;
            return (DataProvider)objProvider[key];
        }

        public void BeginTransaction()
        {
            
            if ( connection == null )
            {
                connection = new SqlConnection(connectionString);
                connection.Open();
            }
            if ( transaction == null )
                transaction = connection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            if ( transaction == null )
                transaction.Commit();
        }

        public void RollbackTransaction()
        {
            if (transaction == null)
                transaction.Rollback();
        }

        public void DisableTransaction()
        {
            if ( connection != null )
                connection.Close();
            if ( transaction == null )
                transaction.Dispose();
        }

        public DataSet ExecuteDataset(string commandText)
        {
            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, commandText);
        }

        public DataSet ExecuteDataset(string commandText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, commandText, commandParameters);
        }
        public DataSet ExecuteDataset(string commandText, int startRecord,int maxRecords,params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteDataset(connectionString,CommandType.Text, commandText, startRecord, maxRecords, commandParameters);
        }
        public DataSet ExecuteDataset(string commandText, CommandType commandType, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteDataset(connectionString, commandType, commandText, commandParameters);
        }

        public int ExecuteNonQuery(string commandText)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, commandText);
        }

        public int ExecuteNonQuery(string commandText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, commandText, commandParameters);
        }

        public int ExecuteNonQuery(string commandText, CommandType commandType, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, commandType, commandText, commandParameters);
        }

        public string ExecuteNonQuery(string commandText, CommandType commandType, string MSG, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, commandType, commandText, MSG, commandParameters);
        }

        public int ExecuteNonQuery(string commandText, CommandType commandType)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, commandType, commandText);
        }

        public int ExecuteNonQueryTransaction(CommandType commandType, string commandText)
        {
            return SqlHelper.ExecuteNonQuery(transaction, commandType, commandText);
        }

        public int ExecuteNonQueryTransaction(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(transaction, commandType, commandText, commandParameters);
        }

        public int ExecuteNonQueryTransaction(string spName, params object[] parameterValues)
        {
            return SqlHelper.ExecuteNonQuery(transaction, spName, parameterValues);
        }

        public object ExecuteScalar(string commandText)
        {
            return SqlHelper.ExecuteScalar(connectionString, CommandType.Text, commandText);
        }

        public object ExecuteScalar(string commandText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteScalar(connectionString, CommandType.Text, commandText, commandParameters);
        }

        public int ExecuteNonQueryByProcedure(string spName, params SqlParameter[] parameterValues)
        {
            return SqlHelper.ExecuteNonQuery(connectionString, spName, parameterValues);
        }

    }
}